/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.DepositDetails;

/**
 * @author ZhangCheng
 * @time 2019年9月18日
 */
public class DepositDetailsDAO extends BasicDAO<DepositDetails> {
	/**
	 * DepositDetailsDAO无参构造 重载父类构造器
	 */
	public DepositDetailsDAO() {
		super();
	}

	/**
	 * DepositDetailsDAO有参构造 重载父类构造器
	 */
	public DepositDetailsDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * 新增新质押物，并插入到数据库中，参数是DepositDetails对象
	 */
	public int insertNewDeposit(DepositDetails dep) {
		int count = 0;
		String sql = "Insert into DepositDetails values(?,?,?,?,?,?,?,?,?)";
		try {
			count = super.updateDML(sql, dep.getDepositId(), dep.getCusId(), dep.getManId(), dep.getDepName(),
					dep.getDepNum(),dep.getDepGuessPrice(),dep.getDepEnsurePrice(), dep.getOkDate(), 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * @apiNote 根据质押物id删除质押物 当数据库内不存在该质押物时 返回 0 此时代表已被删除或者根本不存在 若要判断是其中哪一种状态
	 *          请使用get方法并设置excludeDelete为false
	 */
	public int deleteDepositById(int depositId) {
		int count = 0;
		String sql = "update manager set isDelete = 1 where depositId = ?";
		try {
			count = super.updateDML(sql, depositId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * getDepositDetailById(String depositId,boolean excludeDelete)
	 * 通过质押物编号获取该质押物的所有信息
	 */
	public DepositDetails getDepositDetailByDepositId(int depositId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from DepositDetails where depositId = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<DepositDetails> depD = getInfoFromDB(sql.toString(), depositId);
		if (depD.size() == 0) {
			return null;
		} else {
			return depD.get(0);
		}

	}
	/**
	 * getDepositDetailByManId(String manId,boolean excludeDelete)
	 * 通过经手经理编号获取其管理的质押物的所有信息
	 */
	public ArrayList<DepositDetails> getDepositDetailsByManId(int manId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from DepositDetails where manId = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<DepositDetails> depD = getInfoFromDB(sql.toString(), manId);
		if (depD.size() == 0) {
			return null;
		} else {
			return depD;
		}

	}
	/**
	 * 根据cusId查询正在执行的质押物订单
	 * 
	 * @return 查询不到返回null
	 */
	public ArrayList<DepositDetails> getDepositDetailsByCusId(int cusId, boolean excludeDelete){
		StringBuffer sql = new StringBuffer( "SELECT * FROM DepositDetails where cusId = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<DepositDetails> depD = getInfoFromDB(sql.toString(), cusId);
		if (depD.size() == 0) {
			return null;
		} else {
			return depD;
		}
	}

	/**
	 * getDepositDetailssBySQLFromDB(String sql,Object...obj)
	 * 通过自行传入sql语句以及可变参数获取该sql语句需要进行操作的变量 来进行查询
	 * 
	 * 返回一个DepositDetails类的集合
	 * 
	 * @apiNote 注意：只能传入SELECT * 的语句，否则会报参数不匹配异常
	 */
	public ArrayList<DepositDetails> getDepositDetailssBySQLFromDB(String sql, Object... obj) {
		return getInfoFromDB(sql, obj);
	}

	@Override
	public ArrayList<DepositDetails> resultSetToArrayList(ResultSet re) {
		ArrayList<DepositDetails> d = new ArrayList<DepositDetails>();

		try {
			while (re.next()) {
				d.add(new DepositDetails(re.getString("depositId"), re.getInt("cusId"), re.getInt("manId"),
						re.getString("depName"), re.getInt("depNum"),re.getLong("depGuessPrice"),re.getLong("depEnsurePrice"), re.getDate("okDate")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return d;
	}

}
